<template>
	<Modal
		:mask-closable="false"
		v-model="createStatus"
		:title="type == 'check' ? '查看消息' : type == 'edit' ? '编辑工装分类' : '新建工装分类'"
		class="modal-content-item-layout pt16"
		@confirm="confirm"
		width="70%"
	>
		<div class="addProject">
			<Form ref="form" class="formList" :model="formItem" :rules="ruleValidate" :disabled="type == 'check'">
				<div class="content-item">
					<div class="pd16">
						<Row :gutter="20">
							<Col span="8">
								<FormItem label="工厂名称">
									<Cascader clearable :data="orgList" v-model="formItem.factoryCode" change-on-select placeholder="请选择功能位置"> </Cascader>
								</FormItem>
							</Col>
							<Col span="8">
								<FormItem label="工装分类编号" prop="classificationCoding">
									<Input icon="ios-clock-outline" v-model="formItem.classificationCoding" />
								</FormItem>
							</Col>
							<Col span="8">
								<FormItem label="工装分类名称" prop="classificationName">
									<Input icon="ios-clock-outline" v-model="formItem.classificationName" />
								</FormItem>
							</Col>
						</Row>
						<Row :gutter="20">
							<Col span="8">
								<FormItem label="工装属性" prop="equipAttributes">
									<Select v-model="formItem.equipAttributes" :placeholder="`${$t('common.selectInfo')}`">
										<Option v-for="item of wearAttributeList" :value="item.value" :key="item.value" :label="item.value"> </Option>
									</Select>
								</FormItem>
							</Col>
							<Col span="8">
								<FormItem label="是否启用" prop="isEnabled">
									<Select v-model="formItem.isEnabled" :placeholder="`${$t('common.selectInfo')}`">
										<Option value="1" label="是"> </Option>
										<Option value="0" label="否"> </Option>
									</Select>
								</FormItem>
							</Col>
						</Row>
						<Row :gutter="20">
							<Col span="24">
								<FormItem label="备注" prop="remarks">
									<Input type="text" v-model="formItem.remarks" :placeholder="`${$t('common.inputInfo')}`" />
								</FormItem>
							</Col>
						</Row>
					</div>
				</div>
			</Form>
		</div>

		<div slot="footer">
			<Button type="default" @click="createStatus = false">{{ $t('common.Cancle') }}</Button>
			<Button type="primary" @click="addOk('form', 2)">{{ $t('maintenance.Confirmed') }}</Button>
		</div>
	</Modal>
</template>

<script>
import recursion from '@/util/recursion';
export default {
	name: 'addModal',
	data() {
		return {
			createStatus: false,
			addtitle: true,
			orgList: [],
			formItem: {},
			ruleValidate: {
				factoryName: [{ required: true, message: '请输入工厂名称', trigger: 'blur,change' }],
				classificationCoding: [{ required: true, message: '请输入工装分类编号', trigger: 'blur,change' }],
				classificationName: [{ required: true, message: '请输入工装分类名称', trigger: 'blur,change' }],
				equipAttributes: [{ required: true, message: '请输入工装属性', trigger: 'blur,change' }],
				isEnabled: [{ required: true, message: '请选择是否启用', trigger: 'blur,change' }],
				remarks: [{ required: true, message: '请输入备注', trigger: 'blur,change' }],
			},
			noticeDisable: '',
			type: '',
			wearAttributeList: [],
		};
	},
	mounted() {
		this.getWearAttributeList();
		this.getOrgList();
	},
	methods: {
		show(row, type) {
			this.$refs.form.resetFields();
			this.formItem = {};
			if (row) {
				this.formItem = {
					...row,
					isEnabled: '1',
				};
			}
			this.type = type;
			this.createStatus = true;
		},
		getWearAttributeList() {
			this.axios({
				url: '/dms/faultCategory/dictList?name=工装属性',
				method: 'get',
			}).then((res) => {
				if (res.code == 200) {
					this.wearAttributeList = res.data;
				}
			});
		},
		getOrgList() {
			this.axios({
				method: 'get',
				url: '/basic/organization/findListByToken',
				params: { level: 1 },
			}).then((res) => {
				if (res.code === 200) {
					const obj = {
						label: 'name',
						value: 'code',
						children: 'children',
					};
					this.orgList = recursion.convertTree(res.data, obj);
				}
			});
		},
		addOk(name, status) {
			this.$refs[name].validate((valid) => {
				if (valid) {
					if (this.formItem.id) {
						this.axios({
							method: 'post',
							url: '/dms/classification/updateClassification',
							data: {
								...this.formItem,
								notificationMethod: status,
							},
						}).then((res) => {
							if (res.code === 200) {
								this.$emit('confirm');
								this.createStatus = false;
								this.$Message.success(res.msg);
							} else {
								this.$Message.warning({ content: res.msg, duration: 6, closable: true });
							}
						});
					} else {
						this.axios({
							method: 'post',
							url: '/dms/classification/saveClassification',
							data: {
								...this.formItem,
								notificationMethod: status,
							},
						}).then((res) => {
							if (res.code === 200) {
								this.$emit('confirm');
								this.createStatus = false;
								this.$Message.success(res.msg);
							} else {
								this.$Message.warning({ content: res.msg, duration: 6, closable: true });
							}
						});
					}
				}
			});
		},
	},
};
</script>
